/**
 * Copyright (C) 2009-2012, KingHool.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses.
 */
package com.kinghool.smodel.io.bean.xml;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.kinghool.ebos.commons.exception.ErrorCodeRuntimeException;
import com.kinghool.ebos.commons.utils.Assert;
import com.kinghool.ebos.commons.utils.Utils;
import com.kinghool.smodel.ErrorCodes;
import com.kinghool.smodel.io.bean.annotation.AnnotationBeanChannelProvider;
import com.kinghool.smodel.io.bean.annotation.Array;
import com.kinghool.smodel.io.bean.annotation.Bean;
import com.kinghool.smodel.io.bean.annotation.Exclude;

/**
 * 
 * @Author Allen Deng(dengtailin@gmail.com) 
 */
@Bean(name="beans")
public class BeanModelMapping {
	@Array(child="bean")
	private List<BeanModel> beans = Utils.newList();

	@Exclude
	private Map<String, BeanModel> beanCache = Utils.newMap();
	
	public List<BeanModel> getBeans() {
		return beans;
	}

	public void setBeans(List<BeanModel> beans) {
		if (beans == null) {
			beans = new ArrayList<BeanModel>();
		}
		
		this.beans = beans;
		
		beanCache.clear();		
		for (BeanModel bean : beans) {
			beanCache.put(bean.getClassName(), bean);
		}
	}
	
	public void addBean(BeanModel bean) {
		this.beans.add(bean);
		beanCache.put(bean.getClassName(), bean);
	}
	
	public BeanModel getBeanModel(String beanClassName) {
		BeanModel beanModel = beanCache.get(beanClassName);
		
		doInitialize(beanModel, beanClassName);
		
		return beanModel;
	}
	
	private BeanModel doGetBeanModel(String beanClassName) {
		BeanModel beanModel = beanCache.get(beanClassName);
		
		try {
			Class<?> beanClazz = Class.forName(beanClassName);
			
			if (beanModel == null) {
				beanModel = new AnnotationBeanChannelProvider(beanClazz).getBeanModel(beanClazz);
			}
			return beanModel;
		} catch (ClassNotFoundException e) {
			throw new ErrorCodeRuntimeException(ErrorCodes.SMODEL_005.cloneWithArgs(beanClassName), e);
		}	
	}
	
	private void doInitialize(BeanModel beanModel, String beanClassName) {
		try {
			Class<?> beanClazz = Class.forName(beanClassName);
			
			Map<String, Field> fields = Utils.newMap();
			for (Field field : Utils.getDeclaredFieldsRecursively(beanClazz)) {
				fields.put(field.getName(), field);
			}
			
			for (BeanFieldModel bfModel : beanModel.getBeanFields()) {
				fields.remove(bfModel.getFieldName());
				if (bfModel instanceof IBeanModelHolder) {
					IBeanModelHolder beanHolder = (IBeanModelHolder) bfModel;
					if (beanHolder.getChildBeanModel() == null
						&& Assert.isNotNullNotEmpty(beanHolder.getChildClassName())) {
						if (beanModel.getClassName().equals(beanHolder.getChildClassName())) {
							beanHolder.setChildBeanModel(beanModel);
						} else {
							BeanModel childBeanModel = doGetBeanModel(beanHolder.getChildClassName());
							beanHolder.setChildBeanModel(childBeanModel);
							
							doInitialize(childBeanModel, beanHolder.getChildClassName());
						}
					}
				}
			}
			
			//if include all, load the un-defined field
			if (beanModel.isIncludeAll()) {
				for (Field field : fields.values()) {
					AutoBeanFieldModel bfModel = new AutoBeanFieldModel();
					bfModel.setFieldName(field.getName());
					
					beanModel.addBeanField(bfModel);
				}
			}
		} catch (ClassNotFoundException e) {
			throw new ErrorCodeRuntimeException(ErrorCodes.SMODEL_005.cloneWithArgs(beanClassName), e);
		}		
	}
}
